package com.ciaojian.store.controller.franchisees;

import cn.hutool.core.util.ObjectUtil;
import com.ciaojian.core.mapper.CheckUserMapper;
import com.ciaojian.core.model.vo.FranchiseesWorkerCountVo;
import com.ciaojian.core.model.vo.SalesFranchiseesVo;
import com.ciaojian.core.model.vo.WorkerPerformanceVo;
import com.ciaojian.core.util.Result;
import com.ciaojian.store.service.OrderServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Api(tags = "加盟商数据接口")
@RestController
@RequestMapping("/franchisees/data")
public class DataController {
    @Resource
    private OrderServiceImpl orderService;

    @Resource
    private CheckUserMapper checkUserMapper;

    @ApiOperation("加盟商店铺工作台数据")
    @GetMapping("/workbenchData")
    public Result sales(@NotNull(message = "结束时间不能为空") @DateTimeFormat(pattern = "yyyy-MM") Date date,
                        @RequestParam(required = false) Integer storeId,
                        @RequestParam(required = false) Integer orderType) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, + 1);
        calendar.add(Calendar.MINUTE, - 1);
        SalesFranchiseesVo salesFranchiseesVo = new SalesFranchiseesVo();
        Date endDate = calendar.getTime();

        calendar.add(Calendar.MINUTE, + 1);
        calendar.add(Calendar.MONTH, - 1);
        Date startDate = calendar.getTime();

        //本月收入 salesMonth
        salesFranchiseesVo.setSalesMonth(orderService.salesSalesMonth(storeId,startDate, endDate));

        //本月剪发订单 cutMonthCount
        //本月剪发收入 cutMonthSales
        salesFranchiseesVo.setCutMonthCount(orderService.salesCutMonthCount(storeId,startDate, endDate));
        salesFranchiseesVo.setCutMonthSales(orderService.salesCutMonthSales(storeId,startDate, endDate));

        //本月套餐订单 memberMonthCount
        //本月套餐收入 memberMonthSales
        salesFranchiseesVo.setMemberMonthCount(orderService.salesMemberMonthCount(storeId,startDate,endDate));
        salesFranchiseesVo.setMemberMonthSales(orderService.salesMemberMonthSales(storeId,startDate,endDate));

        //今天收入 salesToday
        //今天剪发订单 cutTodayCount
        salesFranchiseesVo.setSalesToday(orderService.salesSalesToday(storeId));
        salesFranchiseesVo.setCutTodayCount(orderService.salesCutTodayCount(storeId));

        //员工累计迟到(月) beLateMonth
        //员工迟到(今天) beLateToday
        salesFranchiseesVo.setBeLateMonth(checkUserMapper.beLeteMonth(storeId, startDate, endDate));
        salesFranchiseesVo.setBeLateToday(checkUserMapper.beLeteToday(storeId));


        return Result.ok(salesFranchiseesVo);
    }

    @ApiOperation("加盟商员工数据")
    @GetMapping("/workerDate")
       public Result workerDate(Integer storeId,
                             @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startDate,
                             @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endDate) {

        List<FranchiseesWorkerCountVo> franchiseesWorkerCountVo = orderService.workerOrdertotal(storeId,startDate,endDate);

        return Result.ok(franchiseesWorkerCountVo);
    }

    @ApiOperation("员工数据")
    @GetMapping("/workerPerformance")
    public Result workerPerformance(Integer workerId,
                             @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startDate,
                             @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endDate) {

        return orderService.workerPerformance(workerId,startDate,endDate);
    }

}
